<?php
set_time_limit ( 12000 );

require_once 'DBStructure.php';

if (isset ( $argc ) && $argc > 1) {
	$db = NULL;
	$db_host = 'localhost';
	$username = 'root';
	$password = '';
	
	for($ii = 1; $ii < $argc; $ii ++) {
		switch ($argv [$ii]) {
			case '-help' :
			case '/?' :
			case '--help' :
				echo <<<EOD
Usage: {$argv[0]} [options] <dbname>
Options:
	--help : show this help message

	[-d  | --db]  <database> (default "$db")
	[-h  | --host] <host:port> (default "$db_host")
	[-u  | --username] <username> (default "$username")
	[-p  | --password] <password> (default "$password")
	[-np | --no-password]

EOD;
				exit ();
			case '--host' :
			case '--db_host' :
			case '-h' :
				$db_host = $argv [++ $ii];
				break;
			case '--username' :
			case '-u' :
				$username = $argv [++ $ii];
				break;
			case '-p' :
			case '--password' :
				$password = $argv [++ $ii];
				break;
			case '-np' :
			case '--no-password' :
				$password = '';
				break;
			case '--db' :
				$db = $argv [++ $ii];
				break;
			default :
				$db = $argv [$ii];
		}
	}
} else {
	$args = array ('db', 'db_host', 'username', 'password' );
	foreach ( $args as $arg ) {
		if (isset ( $_GET [$arg] )) {
			$$arg = $_GET [$arg];
		} else {
			$$arg = FALSE;
		}
	}
}

if ($db && $db_host && $username) {
	define ( "DB_NAME", $db ); //the database from which to generate the code
	define ( "DB_HOST", $db_host ); //the host of the db
	define ( "DB_USERNAME", $username ); //the username for the db
	define ( "DB_PASSWORD", $password ); //the password for the db
	if (isset ( $_GET ['directory'] ) && $_GET ['directory'] != "" && $_GET ['directory'] != NULL) {
		define ( "MADAL_DIRECTORY", $_GET ['directory'] );
	}
	
	//see if the user want's us to NOT reformat class names
	$ignore_class_name_reformatting = false;
	if (isset ( $_GET ['ignore_class_name_reformatting'] ) && $_GET ['ignore_class_name_reformatting'] != "" && $_GET ['ignore_class_name_reformatting'] != NULL) {
		$ignore_class_name_reformatting = $_GET ['ignore_class_name_reformatting'];
	}
	
	try {
		$reader = new DBStructure ( $ignore_class_name_reformatting );		
		echo $reader->writeTablesToPHPAndJavascriptObjects ();
	} catch ( Exception $e ) {
		echo $e;
		var_dump ( debug_backtrace () );
	}
} else {
	?><form action="GenerateMVC.php" method="get">
<table>
	<tr>
		<td>Database Name:</td>
		<td><input type="text" name="db" id="db" value="your database here" />
		</td>
	</tr>
	<tr>
		<td>Database Host:</td>
		<td><input type="text" name="db_host" id="db_host" value="localhost" /></td>
	</tr>
	<tr>
		<td>Database Username:</td>
		<td><input type="text" name="username" id="username" value="root" /></td>
	</tr>
	<tr>
		<td>Database Password:</td>
		<td><input type="password" name="password" id="password" value="" /></td>
	</tr>
	<tr>
		<td>Directory:</td>
		<td><input type="text" name="directory" id="directory" /></br>
		(Leave blank to use default value of &quot;db-name_mvc&quot;)</td>
	</tr>
	<tr
		title="Class names are made singular from plural database table names, check this box to make the generation process skip this step."
	>
		<td>Skip Class Renaming:</td>
		<td><input type="checkbox" name="ignore_class_name_reformatting"
			id="ignore_class_name_reformatting"
		/></td>
	</tr>
	<tr>
		<td></td>
		<td><input type="submit" value="Generate MVC Framework" /></td>
	</tr>
	<tr>
		<td colspan="2"></td>
	</tr>
</table>
Generating your MVC Framework can take several minutes depending on the
size of your database schema, your processor speed, and your harddrive.
</form>
<?php
}
?>